<template>
  <div class="header-container">
    <div class="header-left flex-box-center">
      <el-icon size="20" class="icon" @click="store.commit('collapseMenu')"
        ><Fold
      /></el-icon>
      <ul class="flex-box-center">
        <li
          v-for="(item, index) in selectMenu"
          :key="item.path"
          class="flex-box-center tab"
          :class="{ selected: route.path === item.path }"
        >
          <el-icon size="12"><component :is="item.icon"></component></el-icon>
          <router-link class="flex-box-center text" :to="{ path: item.path }">
            {{ item.name }}</router-link
          >
          <el-icon size="12" class="tab Close" @click="closeTab(item, index)"
            ><Close
          /></el-icon>
        </li>
      </ul>
    </div>
    <div class="header right">
      <el-dropdown @command="handleClick">
        <div class="el-dropdown-link flex-box-center">
          <el-avatar
            src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
          />
          <p>admin</p>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="cancel">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>
<script setup>
import { computed } from "vue";
import { useStore } from "vuex";
import { useRoute, useRouter } from "vue-router";
// 当前路由对象
const route = useRoute();
const router = useRouter();
const store = useStore();
const selectMenu = computed(() => store.state.menu.selectMenu);
// 点击关闭tag
const closeTab = (item, index) => {
  store.commit("closeMenu", item);
  // 删除非当前页面
  if (route.path !== item.path) {
    return;
  }
  const selectMenuData = selectMenu.value;
  // 删除最后一项
  if (index === selectMenuData.length) {
    // 如果只有一个元素
    if (!selectMenuData.length) {
      router.push("/");
    } else {
      router.push({
        path: selectMenuData[index - 1].path,
      });
    }
  } else {
    // 如果删除中间位置
    router.push({
      path: selectMenuData[index].path,
    });
  }
};
const handleClick = (command) => {
  if (command === "cancel") {
    localStorage.removeItem("pz_token");
    localStorage.removeItem("pz_userInfo");
    localStorage.removeItem("pc_pz");
    window.location.href = window.location.origin;
  }
};
</script>
<style lang="less" scoped>
.flex-box-center {
  display: flex;
  align-items: center;
  height: 100%;
}
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  background-color: #fff;
  padding-right: 25px;
  .header-left {
    height: 100%;
    .icon {
      width: 45px;
      height: 100%;
    }
    .icon:hover {
      background-color: #f5f5f5;
    }
  }
}
.tab {
  padding: 0 5px;
  height: 100%;
  .Close {
    visibility: hidden;
  }
}
.selected {
  background-color: #f5f5f5;
  a {
    color: #409eff;
  }
  i {
    color: #409eff;
  }
}
.tab:hover {
  .Close {
    visibility: inherit;
    color: #333;
  }
}
.text {
  margin: 0 5px;
}
a {
  height: 100%;
  color: #333;
  font-size: 15px;
}
</style>
